package com.th.apicenter.kuaishou.service;

import com.th.apicenter.baidu.entity.ThBaiduData;
import com.th.apicenter.config.ApplicationProperties;
import com.th.apicenter.deduct.entity.ThDeduct;
import com.th.apicenter.mi.entity.ThMiData;
import com.th.apicenter.tools.OkhttpClient;
import com.th.apicenter.tools.ThTools;
import com.th.apicenter.toutiao.entity.ThTtData;
import com.th.apicenter.vivo.entity.ThVivoData;
import com.th.apicenter.xinlang.entity.ThXlData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Service
@Slf4j
public class KuaiShouService {

    @Resource
    private ApplicationProperties applicationProperties;

    private final String ks_xl_callback_url = "http://xlapi.tinghongzz.com/ks/xl/callback";
    private final String ks_xl_callback_active_url = "http://xlapi.tinghongzz.com/ks/xl/callback_active";
    private final String ks_mi_callback_url = "https://apibiz.tinghongzz.com/ks/mi/callback";
    private final String ks_vivo_callback_url = "https://apibiz.tinghongzz.com/ks/vivo/callback";
    private final String ks_toutiao_callback_url = "https://apibiz.tinghongzz.com/ks/toutiao/callback";
    private final String ks_baidu_callback_url = "https://apibiz.tinghongzz.com/ks/baidu/callback";

    private final String xl_ks_adid = "59584";

    /**
     * 点击接收接口，新浪媒体
     */
    public void clickXL(ThXlData thXlData, String type) {
        //拼接请求的url
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(applicationProperties.getKuaishou().getBaseUrl());
        stringBuffer.append(applicationProperties.getKuaishou().getClickUrl());
        stringBuffer.append("?");
        stringBuffer.append("adid=" + xl_ks_adid);
        if (!ThTools.isNull(thXlData.getImei())) {
            stringBuffer.append("&imei=" + thXlData.getImei());
        }
        if (!ThTools.isNull(thXlData.getIdfa())) {
            stringBuffer.append("&idfa=" + thXlData.getIdfa());
        }
        if (!ThTools.isNull(thXlData.getOaid())) {
            stringBuffer.append("&oaid=" + thXlData.getOaid());
        }
        if (!ThTools.isNull(thXlData.getOaidMd5()) && !stringBuffer.toString().contains("oaid=")) {
            stringBuffer.append("&oaid=" + thXlData.getOaidMd5());
        }
        if (!ThTools.isNull(thXlData.getClickId())) {
            stringBuffer.append("&click_id=" + thXlData.getClickId());
        }
        //快手的callback需要编码一次
        String encodedUrl = null;
        try {
            encodedUrl = URLEncoder.encode(type.equals("active") ? ks_xl_callback_active_url : ks_xl_callback_url, "UTF-8").toLowerCase();
            encodedUrl = encodedUrl + "?clickId=" + thXlData.getId();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        stringBuffer.append("&callback=" + encodedUrl);
        log.info("快手点击上传的url:{}", stringBuffer.toString());
        String ksResult = OkhttpClient.httpGet(stringBuffer.toString());
        log.info("快手点击上传的返回:{}", ksResult);
    }

    /**
     * 点击接收接口，小米
     */
    public void clickMi(ThMiData thMiData, ThDeduct thDeduct) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(applicationProperties.getKuaishou().getBaseUrl());
        stringBuffer.append(applicationProperties.getKuaishou().getClickUrl());
        stringBuffer.append("?");
        stringBuffer.append("adid=" + thDeduct.getKsAdid());
        if (!ThTools.isNull(thMiData.getImei())) {
            stringBuffer.append("&imei=" + thMiData.getImei());
        }
        if (!ThTools.isNull(thMiData.getOaid())) {
            stringBuffer.append("&oaid=" + thMiData.getOaid());
        }
        stringBuffer.append("&click_id=mideamijs");
        //快手的callback需要编码一次
        String encodedUrl = null;
        try {
            encodedUrl = URLEncoder.encode(ks_mi_callback_url, "UTF-8").toLowerCase();
            encodedUrl = encodedUrl + "?MiId=" + thMiData.getId();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        stringBuffer.append("&callback=" + encodedUrl);
        log.info("快手点击上传的url:{}", stringBuffer.toString());
        String ksResult = OkhttpClient.httpGet(stringBuffer.toString());
        log.info("快手点击上传的返回:{}", ksResult);
    }


    /**
     * 点击接收接口，vivo
     */
    public void clickVivo(ThVivoData thVivoData, ThDeduct thDeduct) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(applicationProperties.getKuaishou().getBaseUrl());
        stringBuffer.append(applicationProperties.getKuaishou().getClickUrl());
        stringBuffer.append("?");
        stringBuffer.append("adid=" + thDeduct.getKsAdid());
        if (!ThTools.isNull(thVivoData.getImei())) {
            stringBuffer.append("&imei=" + thVivoData.getImei());
        }
        if (!ThTools.isNull(thVivoData.getOaid())) {
            stringBuffer.append("&oaid=" + thVivoData.getOaid());
        }
        //快手的callback需要编码一次
        String encodedUrl = null;
        try {
            encodedUrl = URLEncoder.encode(ks_vivo_callback_url, "UTF-8").toLowerCase();
            encodedUrl = encodedUrl + "?VivoId=" + thVivoData.getId();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        stringBuffer.append("&callback=" + encodedUrl);
        log.info("快手点击上传的url:{}", stringBuffer.toString());
        String ksResult = OkhttpClient.httpGet(stringBuffer.toString());
        log.info("快手点击上传的返回:{}", ksResult);
    }


    /**
     * 点击接收接口，头条
     */
    public void clickToutiao(ThTtData thTtData, String adid) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(applicationProperties.getKuaishou().getBaseUrl());
        stringBuffer.append(applicationProperties.getKuaishou().getClickUrl());
        stringBuffer.append("?");
        stringBuffer.append("adid=" + adid);
        if (!ThTools.isNull(thTtData.getImei())) {
            stringBuffer.append("&imei=" + thTtData.getImei());
        }
        if (!ThTools.isNull(thTtData.getOaid())) {
            stringBuffer.append("&oaid=" + thTtData.getOaid());
        }
        stringBuffer.append("&click_id=" + thTtData.getRequestId());
        //快手的callback需要编码一次
        String encodedUrl = null;
        try {
            encodedUrl = URLEncoder.encode(ks_toutiao_callback_url, "UTF-8").toLowerCase();
            encodedUrl = encodedUrl + "?TTId=" + thTtData.getId();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        stringBuffer.append("&callback=" + encodedUrl);
        log.info("快手点击上传的url:{}", stringBuffer.toString());
        String ksResult = OkhttpClient.httpGet(stringBuffer.toString());
        log.info("快手点击上传的返回:{}", ksResult);
    }


    /**
     * 点击接收接口，百度
     */
    public void clickBaidu(ThBaiduData thBaiduData, ThDeduct thDeduct) {
        StringBuilder stringBuffer = new StringBuilder();
        stringBuffer.append(applicationProperties.getKuaishou().getBaseUrl());
        stringBuffer.append(applicationProperties.getKuaishou().getClickUrl());
        stringBuffer.append("?");
        stringBuffer.append("adid=").append(thDeduct.getKsAdid());
        if (!ThTools.isNull(thBaiduData.getOaid())) {
            stringBuffer.append("&oaid=").append(thBaiduData.getOaid());
        }
        if (!ThTools.isNull(thBaiduData.getImeiMd5())) {
            stringBuffer.append("&imei=").append(thBaiduData.getImeiMd5());
        }
        stringBuffer.append("&click_id=").append(thBaiduData.getClickId());
        //快手的callback需要编码一次
        String encodedUrl = null;
        try {
          //  ks_baidu_callback_url = "https://apibiz.tinghongzz.com/ks/baidu/callback"
            encodedUrl = URLEncoder.encode(ks_baidu_callback_url, "UTF-8").toLowerCase();
            encodedUrl = encodedUrl + "?baiduId=" + thBaiduData.getId();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        stringBuffer.append("&callback=").append(encodedUrl);
        log.info("快手点击上传的url:{}", stringBuffer.toString());
        String ksResult = OkhttpClient.httpGet(stringBuffer.toString());
        log.info("快手点击上传的返回:{}", ksResult);
    }


}
